<template>
  <div class="login-container">
    <div class="header">
      <h3 class="title">Vue电商管理系统</h3>
    </div>

    <div class="wrapper">
      <h2 class="login-title">用户登录</h2>

      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        autocomplete="on"
        label-width="100px"
      >
        <el-form-item prop="username" label="用户名称:">
          <div style="display: flex">
            <el-input
              class="input-frame"
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入用户名称"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
            />
            <span style="width: 15px"></span>
          </div>
        </el-form-item>

        <el-form-item prop="password" label="用户密码:">
          <div style="display: flex">
            <el-input
              class="input-frame"
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入用户密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon
                :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
              />
            </span>
          </div>
        </el-form-item>

        <el-button
          class="login-button"
          :loading="loading"
          type="primary"
          @click.native.prevent="handleLogin"
        >
          登录
        </el-button>
        <br />
        <div>
          <span style="padding-left: 25px">账号 : admin密码 : 随便填</span>
          <span style="padding-left: 25px">账号 : editor 密码 : 随便填</span>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";

export default {
  name: "Login",
  data() {
    // 表单自定义校验规则
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入正确的用户名"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能小于6位"));
      } else {
        callback();
      }
    };
    return {
      // 登录表单数据
      loginForm: {
        username: "admin",
        password: "111111",
      },
      // 表单校验规则
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
      },
      // 密码输入框类型
      passwordType: "password",
      // 登录状态
      loading: false,
      redirect: undefined,
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  methods: {
    // 显示密码类型切换
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "text";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    // 用户登录
    handleLogin() {
      /**
       * 表单校验通过, 该字段返回true, 否则校验不通过返回false
       * @param { Boolean } valid
       */
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery,
              });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!!");
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
  },
};
</script>

<style lang="scss" scoped>
$bg-opacity: rgba(0, 0, 0, 0.35); // 背景透明度

.login-container {
  position: relative;
  width: 100%;
  min-height: 100%;
  background: url("~@/assets/images/login_module/bg.jpg") no-repeat center /
    100% 100%;
  overflow: hidden;

  // 简单的灰色遮罩
  &::before {
    content: "";
    display: block;
    width: 100vw;
    height: 100vh;
    background-color: $bg-opacity;
    zoom: 1; /*兼容IE浏览器6、7低版本*/
    overflow: hidden;
  }

  .header {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 80px;
    background-color: rgba(200, 200, 200, 0.6);
    overflow: hidden;

    .title {
      padding: 0;
      margin: 0;
      height: 80px;
      line-height: 80px;
      margin-left: 200px;
      font-size: 30px;
      text-shadow: #ff0000 5px 5px 5px, #ff0000 -5px -5px 5px;
    }
  }

  .wrapper {
    position: absolute;
    top: 50%;
    right: 10%;
    transform: translate(0, -50%);
    width: 450px;
    background-color: #fff;
    border-radius: 5px;
    padding: 20px 10px 30px;

    .login-title {
      margin: 0;
      padding: 0 0 25px;
      text-align: center;
    }

    .login-form {
      .input-frame {
        margin-right: 10px;
      }
      .show-pwd {
        cursor: pointer;
      }
      .login-button {
        display: block;
        width: 90%;
        margin: auto;
      }
    }
  }
}
</style>
